//
//  KINGStatusCell.swift
//  肉荣微博
//
//  Created by king on 15/8/2.
//  Copyright © 2015年 king. All rights reserved.
//

import UIKit
//控件间距
let statusCellContolMargin = 8.0
class KINGStatusCell: UITableViewCell {
    //如果我给他分配内存空间、他会怎么样
    var status: KINGStatuses? {
        didSet {
            //TODO : - 给属性赋值
            topView.status = status
            messageLabel.text = status?.text
            //设置配图视图数据
            pictureView.status = status
//            messageLabel.text = "丁曹志、生气的小草、于泽、蚂蚱斗士、刘泽、冷山、据说很冷、我就是最帅的 哈哈哈"
            //设置配图视图约束
            pictureHeightLayout?.constant = pictureView.bounds.height
            pictureWidthLayout?.constant = pictureView.bounds.width
        }
    }
    //获取当前cell的高度
    func heightForCell(statusModel:KINGStatuses) -> CGFloat {
        status = statusModel
        layoutIfNeeded()
        return CGRectGetMaxY(bottomView.frame)
    }
    var pictureWidthLayout:NSLayoutConstraint?
    var pictureHeightLayout:NSLayoutConstraint?
    
    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    func setupUI(){
        //一定要在同一控件中进行布局，cell的时候，一定要讲控件添加到contentView中
        contentView.addSubview(topView)
        contentView.addSubview(messageLabel)
        contentView.addSubview(pictureView)
        contentView.addSubview(bottomView)
        contentView.addSubview(separatorView)
        
        //分割视图控件布局
        separatorView.ff_AlignInner(type: ff_AlignType.TopLeft, referView: contentView, size: CGSize(width: UIScreen.mainScreen().bounds.width, height: 10), offset: CGPointZero)
        //顶部视图布局
        topView.ff_AlignVertical(type: ff_AlignType.BottomLeft, referView: separatorView, size: CGSize(width: UIScreen.mainScreen().bounds.width, height: 53), offset: CGPointZero)
        //内容标签布局
        messageLabel.ff_AlignVertical(type: ff_AlignType.BottomLeft, referView: topView, size: nil, offset: CGPoint(x: statusCellContolMargin, y: statusCellContolMargin))
        contentView.addConstraint(NSLayoutConstraint(item: messageLabel, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: contentView, attribute: NSLayoutAttribute.Width, multiplier: 1.0, constant:  -16))
        
//        pictureView.ff_AlignVertical(type: ff_AlignType.BottomLeft, referView: messageLabel, size: nil, offset: CGPoint(x: 0, y: statusCellContolMargin))
//        contentView.addConstraint(NSLayoutConstraint(item: pictureView, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1.0, constant: 290))
//        
//        pictureWidthLayout = contentView.constraints.last
//        contentView.addConstraint(NSLayoutConstraint(item: pictureView, attribute: NSLayoutAttribute.Height, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1.0, constant: 290))
//        pictureHeightLayout = contentView.constraints.last
        
        bottomView.ff_AlignVertical(type: ff_AlignType.BottomLeft, referView: pictureView, size: CGSize(width: UIScreen.mainScreen().bounds.width, height: 44), offset: CGPoint(x: -statusCellContolMargin, y: statusCellContolMargin))
        //不自动计算行高，底部的约束就没用了
//        contentView.addConstraint(NSLayoutConstraint(item: bottomView, attribute: NSLayoutAttribute.Bottom, relatedBy: NSLayoutRelation.Equal, toItem: contentView, attribute: NSLayoutAttribute.Bottom, multiplier: 1.0, constant: 0))
    }
    
    //MARK: - 懒加载控件
    private lazy var separatorView:UIView = {
        let view = UIView()
        view.backgroundColor = UIColor(white: 0.8, alpha: 1.0)
        return view
    }()
    private lazy var topView:KINGStatusCellHeaderView = {
        let view = KINGStatusCellHeaderView()
        view.backgroundColor = UIColor.clearColor()
        return view
    }()
    lazy var messageLabel:UILabel = {
        let msgLabel = UILabel()
        msgLabel.textColor = UIColor.darkGrayColor()
        msgLabel.font = UIFont.systemFontOfSize(14.0)
        msgLabel.preferredMaxLayoutWidth = UIScreen.mainScreen().bounds.width - 2 * CGFloat(statusCellContolMargin)
        msgLabel.numberOfLines = 0
        return msgLabel
    }()

    lazy var pictureView:KINGPictureView = {
        let picture = KINGPictureView()
        return picture
    }()
    lazy var bottomView:KINGStatusCellFooterView = {
        let btmView = KINGStatusCellFooterView()
        btmView.backgroundColor = UIColor(white: 0.9, alpha: 1.0)
        return btmView
    }()
    
}
